Passed
Push — master ( af7af9...a855fd )
by Rasmus
03:53 queued 01:25
created

test-helper.js ➔ verifyLanguageBlock   D

Complexity

Conditions 12

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 27
dl 0
loc 48
rs 4.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like test-helper.js ➔ verifyLanguageBlock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/* eslint-env node */
2
'use strict';
3
4
const assert = require('assert');
5
6
/**
7
 * Verify a language block, divided by two new lines
8
 *
9
 * @param potContents
10
 * @param comment
11
 * @param fileinfo
12
 * @param msgid
13
 * @param plural
14
 * @param context
15
 * @return {boolean}
16
 */
17
function verifyLanguageBlock (potContents, comment, fileinfo, msgid, plural, context) {
18
  const blocks = potContents.split('\n\n');
19
  for (let i = 1; i < blocks.length; i++) {
20
    const blocklines = blocks[i].split('\n');
21
    let fileinfoLine = 0;
22
23
    if (comment && blocks[i].indexOf('#. ' + comment) === -1) {
24
      continue;
25
    } else if (comment) {
26
      fileinfoLine++;
27
    }
28
29
    // Check if correct file
30
    if (fileinfo && blocklines[fileinfoLine].indexOf(fileinfo) === -1) {
31
      continue;
32
    }
33
34
    // Check if msgid is correct
35
    if (msgid && blocks[i].indexOf('msgid "' + msgid + '"\n') === -1) {
36
      continue;
37
    }
38
39
    // Check if plural msgid is correct
40
    if (plural && blocks[i].indexOf('msgid_plural "' + plural + '"\n') === -1) {
41
      continue;
42
    } else if (!plural && blocks[i].indexOf('msgid_plural') !== -1) {
43
      continue;
44
    }
45
46
    // Check if context is correct
47
    if (context && blocks[i].indexOf('msgctxt "' + context + '"\n') === -1) {
48
      continue;
49
    } else if (!context && blocks[i].indexOf('msgctxt') !== -1) {
50
      continue;
51
    }
52
53
    // Check if msgstr is correct when plural
54
    if (plural && blocks[i].indexOf('msgstr[0] ""\n') === -1 && blocks[i].indexOf('msgstr[1] ""\n') === -1 && blocks[i].indexOf('msgstr ""\n') !== -1) {
55
      continue;
56
      // Check if msgstr is correct when singular
57
    } else if (!plural && blocks[i].indexOf('msgstr[0] ""\n') !== -1 && blocks[i].indexOf('msgstr[1] ""\n') !== -1 && blocks[i].indexOf('msgstr ""\n') === -1) {
58
      continue;
59
    }
60
61
    return true;
62
  }
63
  return false;
64
}
65
66
/**
67
 * Test the valid-functions.php file
68
 * Since this file is used many times its a separate function
69
 * @param potContents
70
 * @param fixturePath
71
 * @param invert
72
 */
73
function testValidFunctions (potContents, fixturePath, invert) {
74
  let test = assert;
75
76
  if (invert) {
77
    test = function (value, message) {
78
      assert(!value, message);
79
    };
80
  }
81
82
  test(verifyLanguageBlock(potContents, false, fixturePath + ':2', 'Return string', false, false));
83
  test(verifyLanguageBlock(potContents, false, fixturePath + ':3', 'Print string', false, false));
84
  test(verifyLanguageBlock(potContents, false, fixturePath + ':4', 'Escape for attribute and return string', false, false));
85
  test(verifyLanguageBlock(potContents, false, fixturePath + ':5', 'Escape for attribute and print string', false, false));
86
  test(verifyLanguageBlock(potContents, false, fixturePath + ':6', 'Escape for html and return string', false, false));
87
  test(verifyLanguageBlock(potContents, false, fixturePath + ':7', 'Escape for html and print string', false, false));
88
  test(verifyLanguageBlock(potContents, false, fixturePath + ':8', 'Return string with context', false, 'Some context'));
89
  test(verifyLanguageBlock(potContents, false, fixturePath + ':9', 'Print string with context', false, 'Some context'));
90
  test(verifyLanguageBlock(potContents, false, fixturePath + ':10', 'Escape string with context for attribute', false, 'Some context'));
91
  test(verifyLanguageBlock(potContents, false, fixturePath + ':11', 'Escape string with context for html', false, 'Some context'));
92
  test(verifyLanguageBlock(potContents, false, fixturePath + ':12', 'Singular string', 'Plural string', false));
93
  test(verifyLanguageBlock(potContents, false, fixturePath + ':13', 'Singular string with noop', 'Plural string with noop', false));
94
  test(verifyLanguageBlock(potContents, false, fixturePath + ':14', 'Singular string with context', 'Plural string with context', 'Some context'));
95
  test(verifyLanguageBlock(potContents, false, fixturePath + ':15', 'Singular string with noop and context', 'Plural string with noop and context', 'Some context'));
96
}
97
98
module.exports = {
99
  verifyLanguageBlock: verifyLanguageBlock,
100
  testValidFunctions: testValidFunctions
101
};
102